package com.tfjybj.itoo.exam.provider.controller;

import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.exam.entity.PaperRecordEntity;
import com.tfjybj.itoo.exam.model.CoursePaperRecordModel;
import com.tfjybj.itoo.exam.model.ExamineeModel;
import com.tfjybj.itoo.exam.model.PaperRecordModel;
import com.tfjybj.itoo.exam.provider.service.PaperRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * PaperRecordController
 * paperRecord表
 *
 * @author 任晓帅
 * @version 2.0.0
 * @since 2.0.0 2018-11-15 19:40:28
 */
@Api(tags = {"paperRecord表接口"})
@RequestMapping(value = "/paperRecord")
@RestController
public class PaperRecordController {
    /**
     * 定义打印日志相关
     */
    private static final Logger logger = LoggerFactory.getLogger(PaperRecordController.class);

    @Resource
    private PaperRecordService paperRecordService;

    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model PaperRecordModel
     * @return 添加的结果
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    public ItooResult create(@RequestBody PaperRecordModel model) {
        if (StringUtils.isEmpty(model.getExamineeId())) {
            return ItooResult.build(ItooResult.FAIL, "examineeId为空");
        }
        if (StringUtils.isEmpty(model.getCourseId())) {
            return ItooResult.build(ItooResult.FAIL, "courseId为空");
        }
        if (StringUtils.isEmpty(model.getExaminationId())) {
            return ItooResult.build(ItooResult.FAIL, "examinationId为空");
        }
        if (StringUtils.isEmpty(model.getTemplateId())) {
            return ItooResult.build(ItooResult.FAIL, "templateId为空");
        }
        if (StringUtils.isEmpty(model.getQuestionTypeId())) {
            return ItooResult.build(ItooResult.FAIL, "questionTypeId为空");
        }
        if (StringUtils.isEmpty(model.getQuestionId())) {
            return ItooResult.build(ItooResult.FAIL, "questionId为空");
        }
        if (StringUtils.isEmpty(model.getExamClassifyId())) {
            return ItooResult.build(ItooResult.FAIL, "examClassifyId为空");
        }
        if (StringUtils.isEmpty(model.getStandardScore())) {
            return ItooResult.build(ItooResult.FAIL, "standardScore为空");
        }
        if (StringUtils.isEmpty(model.getMarkStatus())) {
            return ItooResult.build(ItooResult.FAIL, "markStatus为空");
        }
        if (StringUtils.isEmpty(model.getVersion())) {
            return ItooResult.build(ItooResult.FAIL, "version为空");
        }
        PaperRecordEntity paperRecordEntity = new PaperRecordEntity();
        BeanUtils.copyProperties(model, paperRecordEntity);
        paperRecordService.save(paperRecordEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        paperRecordService.removeById(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        paperRecordService.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model PaperRecordModel
     * @return 修改后的结果
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "根据id修改paperRecord")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody PaperRecordModel model) {
        if (StringUtils.isEmpty(model.getExamineeId())) {
            return ItooResult.build(ItooResult.FAIL, "examineeId为空");
        }
        if (StringUtils.isEmpty(model.getCourseId())) {
            return ItooResult.build(ItooResult.FAIL, "courseId为空");
        }
        if (StringUtils.isEmpty(model.getExaminationId())) {
            return ItooResult.build(ItooResult.FAIL, "examinationId为空");
        }
        if (StringUtils.isEmpty(model.getTemplateId())) {
            return ItooResult.build(ItooResult.FAIL, "templateId为空");
        }
        if (StringUtils.isEmpty(model.getQuestionTypeId())) {
            return ItooResult.build(ItooResult.FAIL, "questionTypeId为空");
        }
        if (StringUtils.isEmpty(model.getQuestionId())) {
            return ItooResult.build(ItooResult.FAIL, "questionId为空");
        }
        if (StringUtils.isEmpty(model.getExamClassifyId())) {
            return ItooResult.build(ItooResult.FAIL, "examClassifyId为空");
        }
        if (StringUtils.isEmpty(model.getStandardScore())) {
            return ItooResult.build(ItooResult.FAIL, "standardScore为空");
        }
        if (StringUtils.isEmpty(model.getMarkStatus())) {
            return ItooResult.build(ItooResult.FAIL, "markStatus为空");
        }
        if (StringUtils.isEmpty(model.getVersion())) {
            return ItooResult.build(ItooResult.FAIL, "version为空");
        }
        PaperRecordEntity paperRecordEntity = new PaperRecordEntity();
        BeanUtils.copyProperties(model, paperRecordEntity);
        paperRecordService.updateById(paperRecordEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }

    /**
     * 根据id查找PaperRecord
     *
     * @param id 主键id
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        PaperRecordEntity paperRecordEntity = paperRecordService.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", paperRecordEntity);
    }

    /**
     * 分页查询所有PaperRecord
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "分页查询所有PaperRecord")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                   @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<PaperRecordEntity> paperRecords = paperRecordService.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", paperRecords);
    }

    /**
     * 根据考生_id模糊查询paperRecord
     *
     * @param examineeId 考生_id
     * @param pageNo     页码
     * @param pageSize   页数
     * @return 模糊查询的paperRecord
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "根据考生_id模糊查询paperRecord", notes = "分页根据考生_id模糊查询paperRecord")
    @GetMapping(value = "queryPaperRecordByLikeExamineeId/{pageNo}/{pageSize}")
    public ItooResult queryPaperRecordByLikeExamineeId(@RequestParam(required = false, defaultValue = "") String examineeId,
                                                       @ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                                       @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<PaperRecordEntity> paperRecordList = paperRecordService.queryByLikeExamineeId(examineeId, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", paperRecordList);
    }
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */

    /**
     * 查正在考试抽到错题的考生
     *
     * @param questionId 题的Id
     * @return 抽到错题的考生集合
     * @author 任蒙蒙
     * @since 2.0.0 2018-12-17 19:10:28
     */
    @ApiOperation(value = "抽到错题的考生集合", notes = "分页根据试题_id查询examinee")
    @GetMapping(value = "/selectExamineeByQuestionId/{questionId}")
    public ItooResult selectExamineeByQuestionId(@PathVariable String questionId) {
        if (StringUtils.isEmpty(questionId)) {
            return ItooResult.build(ItooResult.FAIL, "questionId不能为空！");
        }
        List<ExamineeModel> examineeList = paperRecordService.selectExamineeByQuestionId(questionId);
        if (ObjectUtils.isEmpty(examineeList)) {
            return ItooResult.build(ItooResult.FAIL, "抽到错题的考生集合失败{selectExamineeByQuestionId}信息为空");
        } else {
            return ItooResult.build(ItooResult.SUCCESS, "抽到错题的考生集合成功", examineeList);
        }
    }


    /**
     * 查询考生答题记录id
     *
     * @param exameeId 考生的Id
     * @return 答题记录id
     * @author 王雪芬
     * @since 2.0.0 2019-01-23 15:30:00
     */
    @ApiOperation(value = "查询考生答题记录id")
    @GetMapping(value = "/selectPaperRecordId/{exameeId}/{examinationId}")
    public ItooResult selectPaperRecordId(@PathVariable String exameeId, @PathVariable String examinationId) {
        if (StringUtils.isEmpty(exameeId)) {
            return ItooResult.build(ItooResult.FAIL, "exameeId,不能为空！");
        }
        if (StringUtils.isEmpty(examinationId)) {
            return ItooResult.build(ItooResult.FAIL, "examinationId,不能为空！");
        }
        List<String> examineeList = paperRecordService.queryPaperRecordId(exameeId, examinationId);
        return ItooResult.build(ItooResult.SUCCESS, "查询考生答题记录id成功", examineeList);
    }


    /**
     * 根据学生id和试卷id查询小题详情
     *
     * @return 返回根据学生id和试卷id查出小题的详情
     * @author maying
     * @since 2018-11-27 08:10:38
     */
    @ApiOperation(value = "返回根据学生id和试卷id查出小题的详情")
    @GetMapping(value = "/selStuDetails/{examineeId}/{templateId}")
    public ItooResult selStuDetails(@PathVariable String examineeId, @PathVariable String templateId) {
        if (StringUtils.isEmpty(examineeId) || StringUtils.isEmpty(templateId)) {
            return ItooResult.build(ItooResult.FAIL, "id不能为空！");
        }
        try {
            return ItooResult.build(ItooResult.SUCCESS, "查询成功！", paperRecordService.selStuDetails(examineeId, templateId));
        } catch (Exception e) {
            return ItooResult.build(ItooResult.FAIL, "查询失败！");
        }
    }

    /**
     * 查询考试所有试题的正确率
     *
     * @param examID 考试ID
     * @return
     * @author 魏恩君
     * @since 2.0.0 2019/1/26
     */
    @ApiOperation(value = "查询考试所有试题的正确率")
    @GetMapping(value = "/queryQuestionCorrectRate/{examID}")
    public ItooResult queryQuestionCorrectRate(@PathVariable String examID) {
        if (StringUtils.isEmpty(examID)) {
            logger.error("随堂小练查询考试所有试题正确率的考试id为空");
            return ItooResult.build(ItooResult.FAIL, "考试ID不能为空");
        }
        List<PaperRecordModel> list = paperRecordService.queryQuestionCorrectRate(examID);
        if (list != null) {
            return ItooResult.build(ItooResult.SUCCESS, "查询成功", list);
        } else {
            return ItooResult.build(ItooResult.FAIL, "查询失败");
        }
    }

//    /**
//     * 根据记录id更新判分状态，学生得分
//     *
//     * @param id 记录id
//     * @param score 学生得分
//     * @return 是否更新成功
//     * @author 樊建华
//     * @since 2018年11月20日19:40:02
//     */
//
//    @ApiOperation("根据记录id更新判分状态，学生得分")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id",value = "答题记录id"),
//            @ApiImplicitParam(name="score",value = "学生得分")
//    })
//    @ResponseBody
//    @GetMapping(value = "/updateJudgeScore/{id}/{score}")
//    public ItooResult updateJudgeScore(@PathVariable String id,
//                                       @PathVariable double score) {
//        try {
//            Boolean isSuccess = paperRecordService.updateJudgeScore(id, score);
//            if (isSuccess) {
//                return ItooResult.build("0000", "根据记录id更新判分状态，学生得分成功", true);
//            } else {
//                return ItooResult.build("1111", "根据记录id更新判分状态，学生得分结果失败");
//            }
//        } catch (Exception e) {
//            logger.error("根据记录id更新判分状态，学生得分失败", e);
//            return ItooResult.build("1111", "根据记录id更新判分状态，学生得分结果失败", e);
//        }
//    }

    /**
     * 根据试卷id+学生id回显手动判分部分的题目详情
     *
     * @param paperId   试卷id
     * @param studentId 学生id
     * @return 试卷详情
     * @author 樊建华
     * @since 2018年11月21日15:58:40
     */

//    @ResponseBody
//    @RequestMapping(value = {"/queryNotAutoJudeMainsDetails/{paperId}/{studentId}"}, method = RequestMethod.GET)
//    @ApiOperation("根据试卷id+学生id回显手动判分部分的题目详情")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name="paperId",value = "试卷id",required = true,paramType = "path"),
//            @ApiImplicitParam(name="studentId",value = "学生id", required = true,paramType = "path")
//    })
//    public ItooResult queryNotAutoJudeMainsDetails(@PathVariable String paperId, @PathVariable String studentId) {
//        if (com.alibaba.dubbo.common.utils.StringUtils.isEmpty(paperId) || com.alibaba.dubbo.common.utils.StringUtils.isEmpty(studentId)) {
//            return ItooResult.build("1111", "请选择学号和试卷后重试");
//        }
//        try {
//            return paperRecordService.queryNotAutoJudeMainsDetails(paperId, studentId);
//        } catch (Exception e) {
//            logger.error("queryNotAutoJudeMainsDetails--error", e);
//            return ItooResult.build("1111", "操作失败，请重试");
//        }
//    }


    /**
     * 课程练习-根据用户id查询该用户做题记录
     *
     * @param examineeId
     * @return 是否查询成功
     * @ahthor 贾建清
     * @since 2.0.0 2019-3-27 09:05:19
     */
    @ApiOperation(value = "课程练习-根据用户id查询该用户做题记录")
    @GetMapping(value = "/queryQuestionStudentAnswer/{examineeId}")
    public ItooResult queryQuestionStudentAnswer(@PathVariable String examineeId) {
        List<CoursePaperRecordModel> coursePaperRecordModelList = paperRecordService.queryQuestionStudentAnswer(examineeId);
        if (coursePaperRecordModelList != null) {
            return ItooResult.build(ItooResult.SUCCESS, "查询成功", coursePaperRecordModelList);
        } else {
            return ItooResult.build(ItooResult.FAIL, "查询失败");
        }
    }


    /**
     * 课程练习-根据用户id和题干id更新用户答案
     *
     * @param examineeId
     * @param questionId
     * @param coursePaperRecordModel
     * @return 是否更新成功
     * @anthor 贾建清
     * @since 2.0.0 2019-3-27 10:58:24
     */
    @ApiOperation(value = "课程练习-根据用户id和题干id更新用户答案")
    @PutMapping(value = {"/updateStudentAnswer/{examineeId}/{questionId}"})
    public ItooResult updateStudentAnswer(@PathVariable String examineeId, @PathVariable String questionId, @RequestBody CoursePaperRecordModel coursePaperRecordModel) {
        if (StringUtils.isEmpty(examineeId) && StringUtils.isEmpty(questionId) && StringUtils.isEmpty(coursePaperRecordModel)) {
            return ItooResult.build(ItooResult.FAIL, "更新失败", null);
        } else {
            boolean isUpdateStudentAnswer = paperRecordService.updateStudentAnswer(examineeId, questionId, coursePaperRecordModel);
            return ItooResult.build(ItooResult.SUCCESS, "更新成功", isUpdateStudentAnswer);
        }

    }

}
